package cn.bidlink.codegen.core;

import cn.bidlink.codegen.dto.ColumnModelDTO;
import cn.bidlink.codegen.util.Utlis;
import org.apache.velocity.VelocityContext;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * The type Gen service.
 *
 * @author :<a href="mailto:guolongzhang@ebnew.com">张国龙</a>
 * @date :2017-05-25 13:33:31
 */
public class GenService {

    /**
     * 上下文对象
     */
    private VelocityContext ctx;

    /**
     * 配置对象
     */
    private Configeration configeration;

    /**
     * Gen xml.
     *
     * @param outPathName the out path name
     */
    public void genXml(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/MapperXml.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genXml()生成完毕");
    }
    public void genDubboService(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/DubboService.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genDubboService()生成完毕");
    }
    public void genDubboServiceImpl(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/DubboServiceImpl.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genDubboServiceImpl()生成完毕");
    }
    public void genCloudService(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/CloudService.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genCloudervice()生成完毕");
    }
    public void genCloudServiceImpl(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/CloudServiceImpl.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genCloudServiceImpl()生成完毕");
    }
    public void genCloudAddPrams(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/CloudInsertParams.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genCloudAddPrams()生成完毕");
    }
    public void genCloudQueryParams(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/CloudQueryParams.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genCloudQueryParams()生成完毕");
    }
    public void genCloudUpdateParams(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/CloudUpdateParams.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genCloudUpdateParams()生成完毕");
    }
    public void genCloudResponse(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/CloudResponse.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genCloudResponse()生成完毕");
    }
    public void genCloudController(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/CloudController.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genCloudController()生成完毕");
    }
    public void genAppController(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/AppController.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genAppController()生成完毕");
    }
    public void genCloudFeignService(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/CloudFeignService.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genCloudFeignService()生成完毕");
    }

    /**
     * Gen dao.
     *
     * @param outPathName the out path name
     */
    public void genDao(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/Dao.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genDao()生成完毕");
    }
/**
     * Gen ModelExt.
     *
     * @param outPathName the out path name
     */
    public void genEntityExt(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/ModelExt.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genEntityExt()生成完毕");
    }
/**
     * Gen daoExt.
     *
     * @param outPathName the out path name
     */
    public void genDaoExt(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/DaoExt.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genDaoExt()生成完毕");
    }
/**
     * Gen mapperXmlExt.
     *
     * @param outPathName the out path name
     */
    public void genmapperXmlExt(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/MapperExtXml.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genmapperXmlExt()生成完毕");
    }

    /**
     * Gen model.
     *
     * @param outPathName the out path name
     */
    public void genModel(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/Model.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genModel()生成完毕");
    }

    /**
     * Gen enum.
     *
     * @param outPathName the out path name
     */
    public void genEnum(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/Enum.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genEnum()生成完毕");
    }
    public void genSpringConfig(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/spring-config.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genSpringConfig()生成完毕");
    }
    public void genSpringConfig2(String outPathName) {
        try {
            VelocityHelper.generate2(
                    "/template/spring-config2.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genSpringConfig()生成完毕");
    }
    public void genTestDubboService(String outPathName) {
        try {
            VelocityHelper.generate(
                    "/template/TestDubboService.vm",
                    outPathName,
                    getCtx());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("genTestDubboService()生成完毕");
    }

    /**
     * Gets ctx.
     *
     * @return the ctx
     */
    public VelocityContext getCtx() {
        if(ctx==null){
            VelocityContext ctx = new VelocityContext();
            List<ColumnModelDTO> listColumnMode = DataSourceHeper.getTableStructure(configeration.getTableName(), configeration.getDatabaseType());
            String tableComment = DataSourceHeper.getTableRemarks(configeration.getTableName(), configeration.getDatabaseType());
            String testEntityCode = Utlis.getTestEntityCode(listColumnMode,configeration.getModelName(),"entity");
            String testEntityCodeBatch = Utlis.getTestEntityCodeBatch(listColumnMode,configeration.getModelName());
            ctx.put("tableName", configeration.getTableName());
            ctx.put("daoName", configeration.getDaoName());
            ctx.put("xdaoName", Utlis.getLowercaseChar(configeration.getDaoName()));
            ctx.put("xmodelName", Utlis.getLowercaseChar(configeration.getModelName()));
            ctx.put("packageName", configeration.getPackageName());
            ctx.put("DubboServiceImplName", configeration.getDubboServiceImplName());
            ctx.put("DubboServiceName", configeration.getDubboServiceName());
            ctx.put("cloudPackageName", configeration.getCloudPackageName());
            ctx.put("appPackageName", configeration.getAppPackageName());
            ctx.put("cloudServiceName", configeration.getCloudServiceName());
            ctx.put("cloudServiceImplName", configeration.getCloudServiceImplName());
            ctx.put("lowFirstCloudServicelowFirst", Utlis.getLowercaseChar(configeration.getDubboServiceName()));
            ctx.put("modelName", configeration.getModelName());
            ctx.put("lowFirstModelName", Utlis.getLowercaseChar(configeration.getModelName()));
            ctx.put("date", getDate());
            ctx.put("author",configeration.getAuthor());
            ctx.put("listColumnMode", listColumnMode);
            ctx.put("testEntityCode", testEntityCode);
            ctx.put("testEntityCodeBatch", testEntityCodeBatch);
            ctx.put("tableComment", tableComment);
            this.ctx=ctx;
        }
        return this.ctx;
    }

    /**
     * Gets date.
     *
     * @return the date
     */
    public static String getDate() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(new Date());
    }

    public Configeration getConfigeration() {
        return configeration;
    }

    public void setConfigeration(Configeration configeration) {
        this.configeration = configeration;
    }

}
